//
//  WBRadioView.m
//  YJlandlord
//
//  Created by 文彬 on 2018/6/25.
//  Copyright © 2018年 文彬. All rights reserved.
//

#import "WBRadioView.h"

@interface WBRadioView()

@property (strong, nonatomic) UIButton *leftBtn;
@property (strong, nonatomic) UIButton *rightBtn;
@property (strong, nonatomic) UILabel *leftLabel;
@property (strong, nonatomic) UILabel *rightLabel;

@end

@implementation WBRadioView

- (id)initWithLeftTitle:(NSString*)leftTitle rightTitle:(NSString*)rightTitle{
    self = [super init];
    if (self) {
        
        self.leftLabel.text = leftTitle;
        self.rightLabel.text = rightTitle;
        
        [self initControl];
    }
    
    return self;
}
- (void)awakeFromNib{
    [super awakeFromNib];
    
    [self initControl];
}

- (void)initControl{
    [self addSubview:self.leftBtn];
    [self addSubview:self.rightBtn];
    [self addSubview:self.leftLabel];
    [self addSubview:self.rightLabel];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)layoutSubviews{
    
    [self.leftBtn makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@30);
        make.height.equalTo(@30);
        make.left.equalTo(@0);
        make.centerY.equalTo(self.centerY);
    }];
    [self.leftLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftBtn.right).offset(3);
        make.height.equalTo(@30);
        make.centerY.equalTo(self.centerY);
        make.width.equalTo(@(self.frame.size.width/2));
    }];
    
    [self.rightBtn makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@30);
        make.height.equalTo(@30);
        make.left.equalTo(self.centerX);
        make.centerY.equalTo(self.centerY);
    }];
    [self.rightLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.rightBtn.right).offset(3);
        make.height.equalTo(@30);
        make.centerY.equalTo(self.centerY);
        make.width.equalTo(@(self.frame.size.width/2));
    }];
    
}
- (void)setLeftTitle:(NSString*)leftTitle rightTitle:(NSString*)rightTitle
{
    self.leftLabel.text = leftTitle;
    self.rightLabel.text = rightTitle;
}

#pragma mark getter
- (UIButton*)leftBtn{
    if (_leftBtn==nil) {
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftBtn setImage:[UIImage imageNamed:@"radio_checked"] forState:UIControlStateNormal];
        [_leftBtn buttonClick:^{
            if (self.selectIndex == 0) {
                return ;
            }
            [_leftBtn setImage:[UIImage imageNamed:@"radio_checked"] forState:UIControlStateNormal];
            [_rightBtn setImage:[UIImage imageNamed:@"radio_uncheck"] forState:UIControlStateNormal];
            self.selectIndex = 0;
            if (self.valueChanged!=nil) {
                self.valueChanged(@(0));
            }
        }];
    }
    
    return _leftBtn;
}

- (UIButton*)rightBtn{
    if (_rightBtn==nil) {
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightBtn setImage:[UIImage imageNamed:@"radio_uncheck"] forState:UIControlStateNormal];
        [_rightBtn buttonClick:^{
            if (self.selectIndex == 1) {
                return ;
            }
            [_leftBtn setImage:[UIImage imageNamed:@"radio_uncheck"] forState:UIControlStateNormal];
            [_rightBtn setImage:[UIImage imageNamed:@"radio_checked"] forState:UIControlStateNormal];
            self.selectIndex = 1;
            if (self.valueChanged!=nil) {
                self.valueChanged(@(1));
            }
        }];
    }
    
    return _rightBtn;
}

- (UILabel*)leftLabel{
    if (_leftLabel==nil) {
        _leftLabel = [[UILabel alloc]init];
        _leftLabel.font = [UIFont systemFontOfSize:15];
    }
    return _leftLabel;
}

- (UILabel*)rightLabel{
    if (_rightLabel==nil) {
        _rightLabel = [[UILabel alloc]init];
        _rightLabel.font = [UIFont systemFontOfSize:15];
    }
    return _rightLabel;
}

@end
